import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.mysql.jdbc.PreparedStatement;


class EmpAttendForm extends JPanel{
	private static final long serialVersionUID = 1L;
	private  ImageIcon attH = new ImageIcon(this.getClass().getResource("images/header/attend.png"));
	JLabel atte;
	private static AttendancePane attendance = new AttendancePane();

	public EmpAttendForm(){

		atte = new JLabel(attH);
		atte.setBounds(135, 35, attH.getIconWidth(), attH.getIconHeight());
		atte.setVisible(true);

		attendance.setBounds(130, 65, 617, 437);

		add(attendance);
		add(atte);

		setLayout(null);
		setOpaque(false);
		setSize(806,628);
		setVisible(true);

	}

	public  AttendancePane getPane(){
		return attendance;
	}
}


class AttendancePane extends JPanel{
	private static final long serialVersionUID = 1L;
	private JButton search;
	private JLabel frlabel, tolabel;
	private JComboBox smonth, sday, syear, emonth, eday, eyear;
	private JTable tableMod;
	private DefaultTableModel attendList;
	String[] monthItems = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	String[] yearItems = { "2011"};
	int[] intDay = new int[31];
	String[] dayItems = new String[31];

	public AttendancePane() {    
		//construction of table PayrollSummary
		String[] columnNames = {"Date", "Time In", "Time Out", "Status", "Work Hours"};
		tableMod = new JTable();
		tableMod.setModel(new javax.swing.table.DefaultTableModel(null, columnNames){
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return false;
			}
		});
		tableMod.setFillsViewportHeight(true);
		tableMod.getTableHeader().setReorderingAllowed(false);
		attendList = (DefaultTableModel)tableMod.getModel();
		JScrollPane	scrollPane = new JScrollPane();
		scrollPane.setViewportView(tableMod);


		search = new JButton ("Search");
		frlabel = new JLabel ("from:");
		tolabel = new JLabel ("to:");
		smonth = new JComboBox(monthItems);
		sday = new JComboBox();
		syear = new JComboBox(yearItems);
		emonth = new JComboBox(monthItems);
		eday = new JComboBox();
		eyear = new JComboBox(yearItems);

		InitializeDrop(new Integer(31), "start");
		InitializeDrop(new Integer(31), "end");

		//adjust size and set layout
		setPreferredSize (new Dimension (617, 437));
		setLayout (null);
		setOpaque(false);
		setSize(617,437);
		setVisible(true);


		//add components
		add (scrollPane);
		add (search);
		add (frlabel);
		add (tolabel);
		add (smonth);
		add (sday);
		add (syear);
		add (emonth);
		add (eday);
		add (eyear);

		search.setForeground(Color.WHITE);
		search.setBackground(Color.BLACK);

		//set component bounds (only needed by Absolute Positioning)
		search.setBounds (285, 25, 100, 25);
		frlabel.setBounds (20, 25, 35, 25);
		smonth.setBounds(50, 25, 90, 25);
		sday.setBounds(140, 25, 50, 25);
		syear.setBounds(190, 25, 80, 25);
		tolabel.setBounds(20, 55, 20, 25);
		emonth.setBounds(50, 55, 90, 25);
		eday.setBounds(140, 55, 50, 25);
		eyear.setBounds(190, 55, 80, 25);
		scrollPane.setBounds(20, 85, 590, 550);

		smonth.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent e){

				boolean leap = false;

				leap = CheckLeap(Integer.parseInt(syear.getSelectedItem().toString()));

				if(smonth.getSelectedIndex() == 1)
				{
					if(leap==false)
						InitializeDrop(28, "start");
					else
						InitializeDrop(29, "start"); 
				}
				else if (smonth.getSelectedIndex() == 0 || smonth.getSelectedIndex() == 2 || smonth.getSelectedIndex() == 4 
						|| smonth.getSelectedIndex() == 6 || smonth.getSelectedIndex() == 7 || smonth.getSelectedIndex() == 9
						|| smonth.getSelectedIndex() == 11)
					InitializeDrop(31, "start");
				else if (smonth.getSelectedIndex() == 3 || smonth.getSelectedIndex() == 5 || smonth.getSelectedIndex() == 8
						|| smonth.getSelectedIndex() == 10)
					InitializeDrop(30, "start");
			}
		}
		);

		emonth.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent e){

				boolean leap = false;

				leap = CheckLeap(Integer.parseInt(eyear.getSelectedItem().toString()));

				if(emonth.getSelectedIndex() == 1)
				{
					if(leap==false)
						InitializeDrop(28, "end");
					else
						InitializeDrop(29, "end"); 
				}
				else if (emonth.getSelectedIndex() == 0 || emonth.getSelectedIndex() == 2 || emonth.getSelectedIndex() == 4 
						|| emonth.getSelectedIndex() == 6 || emonth.getSelectedIndex() == 7 || emonth.getSelectedIndex() == 9
						|| emonth.getSelectedIndex() == 11)
					InitializeDrop(31, "end");
				else if (emonth.getSelectedIndex() == 3 || emonth.getSelectedIndex() == 5 || emonth.getSelectedIndex() == 8
						|| emonth.getSelectedIndex() == 10)
					InitializeDrop(30, "end");
			}
		}
		);
		
		search.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				attendUpdate();
			}
			
		});
	}

	public void InitializeDrop(int ctr, String type) {
		if(type.equals("start")){
			sday.removeAllItems();

			for(int a = 1; a <= ctr; a++ )
			{     
				intDay[a-1] = a;
				dayItems[a-1] = Integer.toString(intDay[a-1]);
				sday.addItem(dayItems[a-1]);
			}
			sday.repaint();
		}
		else if(type.equals("end")){
			eday.removeAllItems();
			for(int a = 1; a <= ctr; a++ )
			{     
				intDay[a-1] = a;
				dayItems[a-1] = Integer.toString(intDay[a-1]);
				eday.addItem(dayItems[a-1]);
			}

			eday.repaint();

		}
	}


	public boolean CheckLeap(int year) {
		if(year%4 == 0)
		{
			if(year%100 != 0)
			{
				return true;
			}
			else
			{
				if(year%400 == 0)
					return true;
				else
					return false;
			}
		}
		return false;
	}



	public void attendUpdate(){

		try
		{
			ConnectionFactory myFactory = ConnectionFactory.getFactory();
			Connection conn = myFactory.getConnection();
			PreparedStatement stat = (PreparedStatement) conn.prepareStatement("SELECT timeIn as 'Time-In', timeOut as 'Time-Out', dateSal as 'Date', s.desc as 'Status', HOUR(TIMEDIFF(timeOut,timeIn)) as 'Work Hours'"+
					"from bernalcompany.attendance a, bernalcompany.employee e, bernalcompany.attendstatus s where a.dateSal >= '"+getSyear()+"-"+getSmonth()+"-"+getSday()+"'"+
					"and a.dateSal <= '"+getEyear()+"-"+getEmonth()+"-"+getEday()+"' and a.idemployee = e.idemployee and s.aStat = a.stat and a.idemployee= "+MainPanel.getUID()); 
			ResultSet res = stat.executeQuery();

			attendList.getDataVector().removeAllElements();
			tableMod.setAutoCreateRowSorter(true);

			while(res.next()){
				attendList.addRow(new Object[]{res.getDate("Date"), res.getTime("Time-In"), res.getTime("Time-Out"), res.getString("Status"), res.getInt("Work Hours")});
				attendList.fireTableDataChanged();
			}

			conn.close();

		}catch(Exception ex){
			ex.printStackTrace();
		}

	}

	public int getSmonth() {
		String mon = smonth.getSelectedItem().toString();
		int con = 0;
		if(mon.equals("January"))
			con = 1;
		else if(mon.equals("February"))
			con = 2;
		else if(mon.equals("March"))
			con = 3;
		else if(mon.equals("April"))
			con = 4;
		else if(mon.equals("May"))
			con = 5;
		else if(mon.equals("June"))
			con = 6;
		else if(mon.equals("July"))
			con = 7;
		else if(mon.equals("August"))
			con = 8;
		else if(mon.equals("September"))
			con = 9;
		else if(mon.equals("October"))
			con = 10;
		else if(mon.equals("November"))
			con = 11;
		else
			con = 12;

		return con;
	}

	public String getSday() {
		return sday.getSelectedItem().toString();
	}

	public String getSyear() {
		return syear.getSelectedItem().toString();
	}

	public int getEmonth() {
		String mon = emonth.getSelectedItem().toString();
		int con = 0;
		if(mon.equals("January"))
			con = 1;
		else if(mon.equals("February"))
			con = 2;
		else if(mon.equals("March"))
			con = 3;
		else if(mon.equals("April"))
			con = 4;
		else if(mon.equals("May"))
			con = 5;
		else if(mon.equals("June"))
			con = 6;
		else if(mon.equals("July"))
			con = 7;
		else if(mon.equals("August"))
			con = 8;
		else if(mon.equals("September"))
			con = 9;
		else if(mon.equals("October"))
			con = 10;
		else if(mon.equals("November"))
			con = 11;
		else
			con = 12;

		return con;
	}

	public String getEday() {
		return eday.getSelectedItem().toString();
	}

	public String getEyear() {
		return eyear.getSelectedItem().toString();
	}

}
